package com.hpjpw.idoapp.controller;

import com.hpjpw.ido.domain.Communication;
import com.hpjpw.ido.domain.Guard;
import com.hpjpw.ido.service.ICommunicationService;
import com.hpjpw.ido.service.IGuardService;
import com.hpjpw.idoapp.domain.SendCommunicationMessageVo;
import com.hpjpw.idoapp.service.IDoCommunicationService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: ruoyi
 * @BelongsPackage: com.hpjpw.idoapp.controller
 * @Author: hpj
 * @CreateTime: 2021-01-05 16:18
 * @Description: 面向用户的聊天Controller
 */
@Api("面向用户的聊天Controller")
@RestController
@RequestMapping("/ido/app/communication")
public class IDoCommunicationController extends BaseController {
    @Autowired
    private IGuardService guardService;
    @Autowired
    private ICommunicationService communicationService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IDoCommunicationService idoCommunicationService;

    @ApiOperation("获取守护者的守护关系详细信息")
    @GetMapping("/guard")
    public AjaxResult getGuard() {
        Guard guard = guardService.selectGuardByGuardianId(SecurityUtils.getUserId());
        if (StringUtils.isNotNull(guard)) {
            AjaxResult result = AjaxResult.success(guard);
            SysUser sysUser = userService.selectUserById(guard.getProtectedId());
            result.put("protectedName" , sysUser.getNickName());
            result.put("protectedAvatar" , sysUser.getAvatar());
            return result;
        }
        return AjaxResult.error("找不到相应的守护关系");
    }

    @ApiOperation("获取被守护者的守护关系详细信息")
    @GetMapping("/protected")
    public AjaxResult getProtected() {
        Guard guard = guardService.selectGuardByProtectedId(SecurityUtils.getUserId());
        if (StringUtils.isNotNull(guard)) {
            return AjaxResult.success(guard);
        }
        return AjaxResult.error("找不到相应的守护关系");
    }

    @ApiOperation("发送聊天消息")
    @PostMapping("/send")
    public AjaxResult sendCommunicationMessage(@RequestBody @Valid SendCommunicationMessageVo sendCommunicationMessageVo) {
        Long userId = SecurityUtils.getUserId();
        Guard guard = null;
        if (sendCommunicationMessageVo.getIsGuardian()) {
            guard = guardService.selectGuardByGuardianId(userId);
        } else {
            guard = guardService.selectGuardByProtectedId(userId);
        }
        if (StringUtils.isNull(guard)) {
            return AjaxResult.error("找不到相应的守护关系");
        }
        Communication communication = new Communication();
        communication.setSenderId(userId);
        communication.setGuardId(guard.getId());
        communication.setContent(sendCommunicationMessageVo.getMessage());
        communication.setTime(new Date());
        communicationService.insertCommunication(communication);
        return AjaxResult.success();
    }

    @GetMapping("/list")
    @ApiOperation("查询聊天消息列表，可带分页参数：pageNum=1&pageSize=10")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isGuardian" , value = "是否是守护者" , required = true, dataType = "Boolean")
    })
    public TableDataInfo list(@RequestParam Boolean isGuardian) {
        Guard guard;
        if (isGuardian) {
            guard = guardService.selectGuardByGuardianId(SecurityUtils.getUserId());
        } else {
            guard = guardService.selectGuardByProtectedId(SecurityUtils.getUserId());
        }
        if (StringUtils.isNull(guard))
            return getDataTable(new ArrayList<Communication>());
        startPage();
        List<Communication> list = communicationService.selectCommunicationByGuardId(guard.getId());
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('ido:guard:add')")
    @PostMapping("/generate")
    @ApiOperation("生成某年级学生的守护关系，生成前会删除原有年级的守护关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "grade" , value = "学生年级" , required = true, dataType = "String")
    })
    public AjaxResult generate(@RequestParam String grade) {
        int de_num = idoCommunicationService.deleteGuard(grade);
        int ge_num = idoCommunicationService.generateGuard(grade);
        AjaxResult result = AjaxResult.success();
        result.put("delete" , "删除" + grade + "级学生的守护关系" + de_num + "条");
        result.put("generate" , "成功生成" + grade + "级学生的守护关系" + ge_num + "条");
        return result;
    }
}
